﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class EditAddressFooter : System.Web.UI.UserControl
    {
        public string companyname;
        public string address;
        public string phone;
        public string fax;
        public string email;
        public int idcompany;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                idcompany = Convert.ToInt32(Request.QueryString["companyid"]);
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectByColumns("tbl_CompanyInfor", " * ", "selectbycolumns");
                companyname = dt.Rows[0]["CompanyInfor_Name"].ToString();
                address = dt.Rows[0]["CompanyInfor_Description"].ToString();
                phone = dt.Rows[0]["CompanyInfor_Mobile"].ToString();
                fax = dt.Rows[0]["CompanyInfor_Fax"].ToString();
                email = dt.Rows[0]["CompanyInfor_Email"].ToString();

                txtNameCompany.Text = companyname;
                txtAddress.Text = address;
                txtPhone.Text = phone;
                txtFax.Text = fax;
                txtEmail.Text = email;
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            if (txtNameCompany.Text != "")
            {
                if (txtAddress.Text != "")
                {
                    if (txtPhone.Text != "")
                    {
                        if (txtFax.Text != "")
                        {
                            if (txtEmail.Text != "")
                            {
                                if (txtNameCompany.Text.Length < 40)
                                {
                                    if (txtAddress.Text.Length < 45)
                                    {
                                        DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[5];
                                        cl[0] = new DAL.DHelpers.Column("CompanyInfor_Name", txtNameCompany.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                        cl[1] = new DAL.DHelpers.Column("CompanyInfor_Description", txtAddress.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                        cl[2] = new DAL.DHelpers.Column("CompanyInfor_Mobile", txtPhone.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                        cl[3] = new DAL.DHelpers.Column("CompanyInfor_Fax", txtFax.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                        cl[4] = new DAL.DHelpers.Column("CompanyInfor_Email", txtFax.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                                        string[] colWhere = new string[1];
                                        colWhere[0] = "CompanyInfor_ID";

                                        object[] colValue = new object[1];
                                        colValue[0] = idcompany;

                                        DAL.DCollector.DBCollector.Instance.Update("tbl_CompanyInfor", cl, colWhere, colValue);
                                        Response.Redirect("/Admin/Index.aspx?panel=7&sub=6");
                                    }
                                    else
                                    {
                                        Response.Write("<script>alert('Địa chỉ công ty tối đa 45 kí tự...')</script>");
                                    }
                                }
                                else
                                {
                                    Response.Write("<script>alert('Tên công ty tối đa 40 kí tự...')</script>");
                                }
                            }
                            else
                            {
                                Response.Write("<script>alert('Vui lòng nhập Email công ty...')</script>");
                            }
                        }
                        else
                        {
                            Response.Write("<script>alert('Vui lòng nhập số Fax công ty...')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Vui lòng nhập số Điện thoại công ty...')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Vui lòng nhập Địa chỉ công ty...')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('Vui lòng nhập Tên công ty...')</script>");
            }
        }
    }
}